# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.contrib.sessions.models import Session
from django.db import models
from main.models import Rest_dish, Rest
from uuid import uuid4
import random
import time

def generateSN():
    sn = time.strftime('%Y%m%d%H%M%S') 
    sn = sn  + '%d' % random.randint(0,100)
    return sn

class Dish_cart(models.Model):
    """
            点菜表，记录每个用户点的菜式
    """
    id = models.CharField(max_length=50, primary_key=True, default=uuid4)
    
    rest = models.ForeignKey(Rest)
    
    #点菜表可以属于一个已经登录的用户，或者一个未登录的用户，此时记录他的session（消费者）
    user = models.ForeignKey(User, blank=True,null=True)
    session = models.ForeignKey(Session, blank=True,null=True)
   
    #是否已经下定单
    state = models.BooleanField(default=False)
    
    reg_time = models.DateTimeField("创建时间", auto_now_add=True)
    mod_date = models.DateTimeField("修改时间", auto_now=True, auto_now_add=True)
    
    def get_total(self):
        total = 0.0
        for i in self.dish_cart_item_set.all():
            total += i.price * i.amount
        return total
    
    def get_total_amount(self):
        amount = 0
        for i in self.dish_cart_item_set.all():
            amount += i.amount
        return amount
    
class Dish_cart_item(models.Model):

    """
            点菜明细表，记录每个用户点的菜式
    """
    id = models.CharField(max_length=50, primary_key=True, default=uuid4)
    dish_cart = models.ForeignKey(Dish_cart)
    rest_dish= models.ForeignKey(Rest_dish, verbose_name="菜名")
    name = models.CharField("菜式名称", max_length="50")
    price = models.FloatField(verbose_name="价钱",default=0.0)
    amount = models.PositiveSmallIntegerField(verbose_name="数量",default=1)

    reg_time = models.DateTimeField("创建时间", auto_now_add=True)
    mod_date = models.DateTimeField("修改时间", auto_now=True, auto_now_add=True)

    def get_dish_total(self):
        return self.price * self.amount; 
    
class Order(models.Model):
    
    """
            订单表
    """
    id = models.CharField(max_length=50, primary_key=True, default=uuid4)
    dish_cart = models.ForeignKey(Dish_cart)
    #消费餐厅
    rest = models.ForeignKey(Rest)
    #消费者
    user = models.ForeignKey(User)
    #订单号规则待定
    sn = models.CharField(max_length=50,verbose_name="订单号",default=generateSN)
    pwd = models.CharField("确认密码", max_length=5)
    total = models.FloatField(verbose_name="总价",default=0.0)
    leave_msg = models.TextField("留言", blank=True, null=True)
    state = models.BooleanField(verbose_name="是否已经消费", default=False)
    
    msg_state = models.BooleanField(verbose_name="是否已发短信通知订餐双方", default=False)
    plan_ct = models.DateTimeField("计划消费时间", auto_now_add=True)
    real_ct = models.DateTimeField("实际消费时间", auto_now_add=True)
    reg_time = models.DateTimeField("订单创建时间", auto_now_add=True)
    mod_date = models.DateTimeField("订单修改时间", auto_now=True, auto_now_add=True)
    
    
    
class Invoice(models.Model):
    """
           发票表 
    """
    id = models.CharField(max_length=50, primary_key=True, default=uuid4)
    order = models.ForeignKey(Order)
    title = models.CharField(max_length=50,verbose_name="抬头")
    content = models.CharField(max_length=100,verbose_name="内容")
    reg_time = models.DateTimeField("创建时间", auto_now_add=True)
    mod_date = models.DateTimeField("修改时间", auto_now=True, auto_now_add=True)
